//
//  KDInforViewController.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/10.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDInforViewController.h"
#import "KDCertificationViewController.h"
#import "UIImageView+WebCache.h"
@interface KDInforViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UITableView     *_tableView;
    NSArray         *_namesArr;
    UIImageView     *_headImageView;
}
@property (nonatomic,strong)UIImageView     *headImageView;
@property (nonatomic,strong)NSDictionary    *verifyDic;

@end

@implementation KDInforViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setNaviLeftButton];
    [self setNaviRightButton:@"保存"];
    [self setNaviTitle:@"个人信息"];
    
    [self createUI];
    _namesArr = [NSArray arrayWithObjects:@"头像",@"姓名",@"性别",@"电话",@"邮箱",@"医院",@"科室",@"职称",@"擅长",@"医师资格认证", nil];
    _tableView = [[UITableView alloc] initWithFrame:(CGRect){0,0,kScreenWidth,kScreenHeight-64} style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate   = self;
    _tableView.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
//    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_tableView];
    
    _cameraView = [[KDCameraView alloc] init];
    _cameraView.cameraViewDelegate = self;
}
- (void)viewWillAppear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = YES;
    [self requestGetUserInfo:@"verify"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = NO;
}
- (void)clickRightButton:(id)sender {
    [self requestUpdateInfoAccredHead_ico:nil doctor_name:[[self getFieldWithTag:1] text] gender:[[self getLabelWithTag:2] text] tel:[[self getFieldWithTag:3] text] email:[[self getFieldWithTag:4] text] hospital_name:[[self getFieldWithTag:5] text] department:[[self getFieldWithTag:6] text] job_name:[[self getFieldWithTag:7] text] begoodat:[[self getFieldWithTag:8] text] card_pic_path:nil work_pic_path:nil];
}
//网络交互
//getUserInfo
-(void)requestGetUserInfo:(NSString *)type
{
    [CSHApiClient getUserInfoType:type didFinishLoaded:^(KXJson *json, NSString *path) {
        //
        if ([[json getStringForKey:@"code"] intValue] == 200) {
            NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:[json getObjectForKey:@"data"]];
            self.verifyDic = dic1;
            [self reloadLabels:_verifyDic];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}
//update
//备注:还不确定头像和证件照片的提交方式
- (void)requestUpdateInfoAccredHead_ico:(NSString *)Head_ico doctor_name:(NSString *)doctor_name gender:(NSString *)gender tel:(NSString *)tel email:(NSString *)email hospital_name:(NSString *)hospital_name department:(NSString *)department job_name:(NSString *)job_name begoodat:(NSString *)begoodat card_pic_path:(NSString *)card_pic_path work_pic_path:(NSString *)work_pic_path
{
    [CSHApiClient updateInfoAccredHead_ico:Head_ico doctor_name:doctor_name gender:gender tel:tel email:email hospital_name:hospital_name department:department job_name:job_name begoodat:begoodat card_pic_path:card_pic_path work_pic_path:work_pic_path didFinishLoaded:^(KXJson *json, NSString *path) {
        //
        if ([[json getStringForKey:@"code"] intValue] == 200) {
            [self showAlert:[json getStringForKey:@"message"]];
        }else{
            [self showAlert:@"操作失败,请重试"];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}

- (void)showAlert:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}
- (void)reloadLabels:(NSDictionary *)verifyDic
{
    if (verifyDic[@"head_ico"]) {
        [self.headImageView setImageWithURLString:verifyDic[@"head_ico"]];
    }
    [[self getFieldWithTag:1] setText:verifyDic[@"doctor_name"]];
    [[self getLabelWithTag:2] setText:verifyDic[@"gender"]];
    [[self getFieldWithTag:3] setText:verifyDic[@"tel"]];
    [[self getFieldWithTag:4] setText:verifyDic[@"email"]];
    [[self getFieldWithTag:5] setText:verifyDic[@"hospital_name"]];
    [[self getFieldWithTag:6] setText:verifyDic[@"department"]];
    [[self getFieldWithTag:7] setText:verifyDic[@"job_name"]];
    [[self getFieldWithTag:8] setText:verifyDic[@"begoodat"]];
    //0审核中，1通过审核，2未通过审核
    if ([verifyDic[@"is_examine"] intValue] == 0) {
        [[self getLabelWithTag:9] setText:@"审核中" ];
    }else if ([verifyDic[@"is_examine"] intValue] == 1){
       [[self getLabelWithTag:9] setText:@"通过审核"];
    }else{
        [[self getLabelWithTag:9] setText:@"未通过审核"];
    }
}

- (UILabel  *)getLabelWithTag:(NSInteger)tag
{
    UILabel *label1 = (UILabel *)[self.view viewWithTag:tag];
    return label1;
}
- (UITextField  *)getFieldWithTag:(NSInteger)tag
{
    UITextField *label1 = (UITextField *)[self.view viewWithTag:tag];
    return label1;
}
- (void)createUI
{
    self.headImageView = [[UIImageView alloc] initWithFrame:(CGRect){kScreenWidth-44-20,5,44,44}];
    [self.headImageView setImage:[UIImage imageNamed:@"ic_pic.png"]];
    self.headImageView.layer.cornerRadius = 5;
    self.headImageView.layer.masksToBounds = YES;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 4;
    }else if (section == 2){
        return 4;
    }else{
        return 1;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 55;
    }else{
        return 44;
    }
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
{
    if(indexPath.section == 0){
        return YES;
    }else if (indexPath.section == 1){
        if(indexPath.row == 1){
            return YES;
        }
    }else if (indexPath.section == 3){
        return YES;
    }
    return NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        [_cameraView showCameraView:self];
    }else if (indexPath.section == 1) {
        
    }else if (indexPath.section == 2){
        
    }else{
        [self.navigationController pushViewController:[[KDCertificationViewController alloc] init] animated:YES];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CustomCellIdentifier = [NSString stringWithFormat:@"CellIdentifier%ld",(long)indexPath.row+indexPath.section*10];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
        if (indexPath.section == 1 && indexPath.row == 1) {
            UIImageView *nextMonthImageView = [[UIImageView alloc] initWithFrame:(CGRect){kScreenWidth-20,17,13/2,21/2}];
            [nextMonthImageView setImage:[UIImage imageNamed:@"btn_next2@2x.png"]];
            [cell.contentView addSubview:nextMonthImageView];
        }else if (indexPath.section == 3 && indexPath.row == 0) {
            UIImageView *nextMonthImageView = [[UIImageView alloc] initWithFrame:(CGRect){kScreenWidth-20,17,13/2,21/2}];
            [nextMonthImageView setImage:[UIImage imageNamed:@"btn_next2@2x.png"]];
            [cell.contentView addSubview:nextMonthImageView];
        }
        
    }
    if (indexPath.section == 0) {
        [cell.textLabel setText:_namesArr.firstObject];
        [cell.contentView addSubview:self.headImageView];
    }else if (indexPath.section == 1) {
        [cell.textLabel setText:[_namesArr objectAtIndex:indexPath.row+1]];
        if (indexPath.row == 1) {
            if (![self.view viewWithTag:indexPath.row+1]) {
                UILabel *label1 = [self createLabel];
                [label1 setTag:indexPath.row+1];
                [cell.contentView addSubview:label1];
            }
            
        }else{
            if (![self.view viewWithTag:indexPath.row+1]) {
            UITextField *field1 = [self createTextField];
            [field1 setTag:indexPath.row + 1];
            [cell.contentView addSubview:field1];
            }
        }
        
    }else if (indexPath.section == 2){
        [cell.textLabel setText:[_namesArr objectAtIndex:indexPath.row+5]];
        if (![self.view viewWithTag:indexPath.row+5]) {
        UITextField *field1 = [self createTextField];
        [field1 setTag:indexPath.row+5];
        [cell.contentView addSubview:field1];
        }
    }else{
        [cell.textLabel setText:_namesArr.lastObject];
        if (![self.view viewWithTag:9]) {
        UILabel *label1 = [self createLabel];
        [label1 setTag:9];
        [cell.contentView addSubview:label1];
            if ([self.verifyDic[@"is_examine"] intValue] == 0) {
                [label1 setText:@"审核中" ];
            }else if ([self.verifyDic[@"is_examine"] intValue] == 1){
                [label1 setText:@"通过审核"];
            }else{
                [label1 setText:@"未通过审核"];
            }
        }
    }
//    [cell.textLabel setTextColor:[UIColor colorWithHexString:@"666666"]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:12]];
    
    
    
    return cell;
}

- (UILabel *)createLabel
{
    UILabel *label = [[UILabel alloc] initWithFrame:(CGRect){kScreenWidth-200,0,175,44}];
    [label setTextAlignment:NSTextAlignmentRight];
    [label setFont:[UIFont systemFontOfSize:12]];
    [label setTextColor:[UIColor colorWithHexString:@"cccccc"]];
    return label;
}

- (UITextField *)createTextField
{
    UITextField *textField = [[UITextField alloc] initWithFrame:(CGRect){kScreenWidth-200,0,175,44}];
    [textField setBorderStyle:UITextBorderStyleNone];
    [textField setTextAlignment:NSTextAlignmentRight];
    [textField setFont:[UIFont systemFontOfSize:12]];
    [textField setTextColor:[UIColor colorWithHexString:@"cccccc"]];
    textField.delegate = self;
    [textField setBackgroundColor:[UIColor clearColor]];
    return textField;
}

- (void)hidenKey
{
    [[self getFieldWithTag:1] resignFirstResponder];
    [[self getFieldWithTag:3] resignFirstResponder];
    [[self getFieldWithTag:4] resignFirstResponder];
    [[self getFieldWithTag:5] resignFirstResponder];
    [[self getFieldWithTag:6] resignFirstResponder];
    [[self getFieldWithTag:7] resignFirstResponder];
    [[self getFieldWithTag:8] resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self hidenKey];
    return YES;
}
//delegate
- (void)chooseImage:(UIImage *)image
{
    [_headImageView setImage:image];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
